package com.ruoyi.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.platform.config.ThreadPoolConfig;
import com.ruoyi.platform.system.domain.SysOperLog;
import com.ruoyi.platform.system.mapper.SysOperLogMapper;
import com.ruoyi.platform.utils.spring.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @Author: lzx
 * @Date: 2022/11/7 15:08
 * @Description: 删除日志
 **/
@EnableScheduling
@Component
@Slf4j
public class AutoDelOperateJob {

    @Scheduled(cron = "0 0 3 * * ?")
    @Async(ThreadPoolConfig.THREAD_NAME)
    public void autoDelOperateJob() {
        SysOperLogMapper sysOperLogMapper = SpringUtils.getBean(SysOperLogMapper.class);
        LambdaQueryWrapper<SysOperLog> query = Wrappers.lambdaQuery();
        query.orderByDesc(SysOperLog::getOperId).last("limit 1");
        SysOperLog newLastedSysOperLog = sysOperLogMapper.selectOne(query);
        query.clear();
        query.lt(SysOperLog::getOperId,newLastedSysOperLog.getOperId()-100_0000);
        sysOperLogMapper.delete(query);
        log.info("日志清理完毕");
    }



}
